package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.ProcessAdjustmentReq;
import com.ruibang.glass.produce.entity.ElasticModulus;
import com.ruibang.glass.produce.entity.ProcessAdjustment;
import com.ruibang.glass.produce.mapper.ProcessAdjustmentMapper;
import com.ruibang.glass.produce.service.ProcessAdjustmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 工艺调整 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-03-15
 */
@Service
public class ProcessAdjustmentServiceImpl extends ServiceImpl<ProcessAdjustmentMapper, ProcessAdjustment> implements ProcessAdjustmentService {

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(ProcessAdjustment::getId, id).set(ProcessAdjustment::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public IPage<ProcessAdjustment> pageQuery(ProcessAdjustmentReq processAdjustmentReq) {
        QueryWrapper<ProcessAdjustment> qw = new QueryWrapper<>();
        qw.lambda().eq(StringUtils.isNotBlank(processAdjustmentReq.getClasses()), ProcessAdjustment::getClasses, processAdjustmentReq.getClasses());
        qw.lambda().like(StringUtils.isNotBlank(processAdjustmentReq.getContent()), ProcessAdjustment::getAdjustContent, processAdjustmentReq.getContent());
        qw.lambda().eq(StringUtils.isNotBlank(processAdjustmentReq.getType()), ProcessAdjustment::getType, processAdjustmentReq.getType());
        qw.lambda().eq(ProcessAdjustment::getIsDelete, Constants.ZERO_STR);
        if (processAdjustmentReq.getStartDate() != null && processAdjustmentReq.getEndDate() != null) {
            qw.lambda().ge(ProcessAdjustment::getDate, processAdjustmentReq.getStartDate());
            qw.lambda().le(ProcessAdjustment::getDate, processAdjustmentReq.getEndDate());
        }
        qw.lambda().orderByDesc(ProcessAdjustment::getCreateTime);
        return this.page(new Page<>(processAdjustmentReq.getPageNo(), processAdjustmentReq.getPageSize()), qw.lambda());
    }

    @Override
    public void insert(ProcessAdjustment processAdjustment) {
        //记录人
        String recorderId = processAdjustment.getRecorderId();
        if (StringUtils.isNotBlank(recorderId)) {
            Object userNameObj = redisService.hget(RedisKey.USER_GROUP_TYPE, recorderId);
            if (userNameObj != null) {
                processAdjustment.setRecorder(userNameObj.toString());
            }
        }
        //下达人
        String issuedById = processAdjustment.getIssuedById();
        if (StringUtils.isNotBlank(issuedById)) {
            Object userNameObj = redisService.hget(RedisKey.USER_GROUP_TYPE, issuedById);
            if (userNameObj != null) {
                processAdjustment.setIssuedBy(userNameObj.toString());
            }
        }
        this.save(processAdjustment);
    }


}
